package cn.cyber.wxfire.service;

import cn.cyber.wxfire.entity.Msg.BaseMsg;
import cn.cyber.wxfire.entity.Msg.TextMsg;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WXChartService {
    public static Map<String, String> parseRequest(InputStream is) {

        Map<String,String> map = new HashMap<String,String>();
        //1.通过io流得到文档对象
        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            document =  saxReader.read(is);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //2.通过文档对象得到根节点对象
        Element root = document.getRootElement();
        //3.通过根节点对象获取所有子节点对象
        List<Element> elements = root.elements();
        //4.将所有节点放入map
        for (Element element : elements) {
            map.put(element.getName(), element.getStringValue());
        }
        return map;
    }

    public static String beanToXml(BaseMsg msg) {
        XStream stream = new XStream();
        stream.processAnnotations(TextMsg.class);

        String xml = stream.toXML(msg);
        return xml;
    }


    public static BaseMsg dealTextMsg(Map<String, String> requestMap) {
        // 获取用户发送的消息内容
        String msg = requestMap.get("Content");
        // 如果是图文回复一个图文消息
        /*if (msg.equals("图文")) {
            List<Article> articles = new ArrayList<Article>();
            articles.add(new Article("码云博客", "这个是我个人的码云博客,基于hexo搭建,里面的文章都是使用markdown编写",
                    "https://heliufang.gitee.io/uploads/banner.jpg", "https://heliufang.gitee.io/"));
            return new NewsMsg(requestMap, articles);
        }
        if(msg.equals("登录")) {
            //引导用户访问这个url,然后访问之后,微信会重定向到我们自己的url
            String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE#wechat_redirect";
            String redirect_uri = "http://www.647x0zj.cn/wx/getUserInfo";
            url = url.replace("APPID", APPID).replace("REDIRECT_URI", redirect_uri).replace("SCOPE", "snsapi_userinfo");
            System.out.println("登录url:"+url);
            return new TextMsg(requestMap, "<a href=\""+url+"\">点击登录</a>");
        }*/

        TextMsg textMsg;
        if(msg.equals("你好")) {
            textMsg = new TextMsg(requestMap, "你也好");
        }
        else if(msg.equals("常用下载")){
            String url1 = "http://xuegong.qfnu.edu.cn:8080/v/content/detail/0ac6bc96-75f7-4496-a343-4096aaf6e238";
            String url2 = "http://xuegong.qfnu.edu.cn:8080/v/content/detail/e4ca271d-014e-4cf9-82bf-ff63610d8121";
            String url3 = "http://xuegong.qfnu.edu.cn:8080/v/content/detail/fae08990-e367-4a95-831b-2f655d028820";
            String url4 = "http://xuegong.qfnu.edu.cn:8080/v/content/detail/d542757b-9d71-4b35-a7d0-c79ae14c2508";
            String url5 = "http://xuegong.qfnu.edu.cn:8080/v/content/detail/ebc139fe-dfd9-4e2b-83c1-7fa5860f6840";
            String url6 = "https://xg.qfnu.edu.cn/xzzx.htm";
            return new TextMsg(requestMap,
                    "常用下载\n" +
                            "<a href=\""+url1+"\">1.2022学年校历</a>"+
                            "\n <a href=\""+url2+"\">2.2022疫情承诺书</a>"+
                            "\n <a href=\""+url3+"\">3.本科生推荐表</a>"+
                            "\n <a href=\""+url4+"\">4.学校信纸</a>"+
                            "\n <a href=\""+url5+"\">5.按期毕业证明</a>\n"+
                            "\n <a href=\""+url6+"\">更多</a>"
            );
        }
        else if(msg.equals("最新政策")){
            String url1 = "https://www.qfnu.edu.cn/info/1035/21150.htm";
            String url2 = "https://www.qfnu.edu.cn/info/1035/21127.htm";
            String url3 = "https://lab.qfnu.edu.cn/info/1019/2976.htm";
            String url4 = "https://www.qfnu.edu.cn/info/1035/21143.htm";
            String url5 = "https://www.qfnu.edu.cn/tzgg.htm";
            return new TextMsg(requestMap,
                            "最新政策\n"+
                            "<a href=\""+url1+"\">1.关于2022年清明节假期调整的通知</a>"+
                            "\n<a href=\""+url2+"\">2.关于召开2022届毕业生就业工作推动会的通知</a>"+
                            "\n<a href=\""+url3+"\">3.关于曲阜校区启用线上实验教学的通知</a>"+
                            "\n<a href=\""+url4+"\">4.关于召开学校党委理论学习中心组学习会的通知</a>\n"+
                            "\n<a href=\""+url5+"\">更多</a>"
            );
        }
        else if(msg.equals("关于我们"))
        {
            textMsg = new TextMsg(requestMap, "我们是数据结构课程设计第四组");
        }
        else
        {
            textMsg = new TextMsg(requestMap, "无法识别。\n请输入\"常用下载\"，\"最新政策\"或\"关于我们\"");
        }
        return textMsg;
    }

    public static String getRespose(Map<String, String> requestMap) {
        BaseMsg msg = null;
        // 根据用户发送消息的类型,做不同的处理
        String msgType = requestMap.get("MsgType");
        switch (msgType) {
            case "text":
                msg = dealTextMsg(requestMap);
                break;
            default:
                break;
        }
        // System.out.println(msg);
        // 将处理结果转化成xml的字符串返回
        if (null != msg) {
            return beanToXml(msg);
        }
        return null;
    }
}
